package cn.itrigger.dynamic.sql.select;

import cn.itrigger.dynamic.sql.render.RenderingStrategy;
import cn.itrigger.dynamic.sql.select.render.SelectStatementProvider;

import java.util.Objects;
import java.util.function.Function;

/**
 * @author admin
 * @date 2018/9/26
 **/
public class MyBatis3SelectModelAdapter<R> {
    private SelectModel selectModel;
    private Function<SelectStatementProvider, R> mapperMethod;

    private MyBatis3SelectModelAdapter(SelectModel selectModel, Function<SelectStatementProvider, R> mapperMethod) {
        this.selectModel = Objects.requireNonNull(selectModel);
        this.mapperMethod = Objects.requireNonNull(mapperMethod);
    }

    public R execute() {
        return mapperMethod.apply(selectStatement());
    }

    private SelectStatementProvider selectStatement() {
        return selectModel.render(RenderingStrategy.MYBATIS3);
    }

    public static <R> MyBatis3SelectModelAdapter<R> of(SelectModel selectModel, Function<SelectStatementProvider, R> mapperMethod) {
        return new MyBatis3SelectModelAdapter<>(selectModel, mapperMethod);
    }
}
